package com.project.controller.web;

import com.project.entity.PositionType;
import com.project.myBeans.ResponseBean;
import com.project.service.PositionTypeService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * author: WheelChen
 * data: 2017/6/26
 * function: 实习岗位类别表
 */
@Controller
@RequestMapping("/PositionType")
public class PositionTypeController {
    @Autowired
    private PositionTypeService positionTypeService;

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/26
     * function: 查找所有实习岗位类别
     *
     * @param
     * @return
     */
    public ResponseBean findAll(){
        ResponseBean responseBean = new ResponseBean();
        try{
            List<PositionType> positionTypes = positionTypeService.findAll();
            responseBean.setData(positionTypes);
            responseBean.setMessage("获取成功");
            responseBean.setSuccess(true);
            return responseBean;
        }catch (Exception e){
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
            return responseBean;
        }
    }

    @RequestMapping(value = "/find/{id}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/29
     * function: 根据id查找岗位类别
     *
     * @param id
     * @return
     */
    public ResponseBean find(@PathVariable("id") Integer id){
        ResponseBean responseBean = new ResponseBean();
        try{
            PositionType positionType = positionTypeService.findById(id);
            responseBean.setData(positionType);
            responseBean.setMessage("查找成功");
            responseBean.setSuccess(true);
            return responseBean;
        }catch (Exception e){
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
            return responseBean;
        }
    }

    @RequestMapping(value = "/findBatchBySchoolId/{schoolId}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/9
     * function: 按照学院id查找岗位类别
     *
     * @param schoolId
     * @return
     */
    public ResponseBean findBatchBySchoolId(@PathVariable("schoolId") Integer schoolId){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.setData(positionTypeService.findBatchBySchoolId(schoolId));
            responseBean.setSuccess(true);
            responseBean.setMessage("查找成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/9
     * function: 添加一个岗位类别
     *
     * @param positionType
     * @return
     */
    public ResponseBean addOne(@RequestBody PositionType positionType){
        ResponseBean responseBean = new ResponseBean();
        try {
            positionTypeService.addOne(positionType);
            responseBean.setSuccess(true);
            responseBean.setMessage("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/deleteOneById/{id}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/9
     * function: 根据id删除一个岗位类别
     *
     * @param id 主键
     * @return
     */
    public ResponseBean deleteOneById(@PathVariable("id") Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (positionTypeService.delete(id) > 0){
                responseBean.setSuccess(true);
                responseBean.setMessage("删除成功");
            }
            else{
                responseBean.setSuccess(false);
                responseBean.setMessage("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/findBatchByCondition", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/9
     * function: [条件][分页]查找岗位类别
     *
     * @param positionType
     * @return
     */
    public ResponseBean findBatchByCondition(@RequestBody PositionType positionType){
        ResponseBean responseBean = new ResponseBean();
        try {
            positionType.setPageIndex(positionType.getPageIndex() - 1);
            List<PositionType> positionTypeList = positionTypeService.findBatchByCondition(positionType);
            responseBean.setData(positionTypeList);
            responseBean.setTotal(positionType.getTotal());
            responseBean.setSuccess(true);
            responseBean.setMessage("查找成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }



        return responseBean;
    }



}
